package com.example.mform.repository;

import com.example.mform.entity.FillForm;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author shishuqian
 * date 2020/12/16
 * time 21:36
 **/
@Repository
public interface FillFormRepository extends JpaRepository<FillForm,Long> {

    /**
     * 使用sql查询
     * nativeQuery = true:使用sql查询
     * nativeQuery = false:使用jpql查询，默认就是false
     */
    @Query(value = "select answer from t_fill_form where question_id = ?",nativeQuery = true)
    List<String> queryAnswer(Long question_id);

    //查询填写指定问卷的人数，忽略user_id为NULL的
    @Query(value = "select count(distinct user_id) from t_fill_form where form_id = ?",nativeQuery = true)
    int countPeople(Long form_id);

    //统计某选项被选的次数
    @Query(value = "select count(answer) from t_fill_form where answer = ?",nativeQuery = true)
    int countChoice(Long answer);
}
